<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;

class Wechatservice extends Controller
{
    public function GetWechatInfo(){

        //系统参数
        $Config = Config();

        // 获取页面URL的CODE参数，判断是否有值
        if(isset($_GET['code'])) {
            // 获取openid和access_token
            $app_id = $Config['wechat_appid'];
            $app_secret = $Config['wechat_appsecret'];
            $code = $_GET['code'];
            // 发送请求，获取用户openid和access_token
            $data = get_by_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$app_id.'&secret='.$app_secret.'&code='.$code.'&grant_type=authorization_code');
            $data = json_decode($data);

            // 防止第二次访问动态链接报错
            // 判断是否获取到当前用户的openid
            if(isset($data->openid)){
                $open_id = $data->openid;
                $access_token = $data->access_token;

                // 获取当前用户信息
                $user_info = get_by_curl('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$open_id.'&lang=zh_CN');
                $user_info = json_decode($user_info);

                // 取出用户信息
                /*
                *user_openid :用户openId
                *user_nickname :用户昵称
                *user_sex :性别
                *user_province :省
                *user_city :城市
                *user_headimgurl :用户头像url
                */
                $user_openid = $user_info->openid;
                $user_nickname = $user_info->nickname;
                $user_sex = $user_info->sex;
                $user_province = $user_info->province;
                $user_city = $user_info->city;
                $user_headimgurl = $user_info->headimgurl;


                // 以下操作可按照自己的需求编写，这里只做例子
                $UserID = Session::get('user_id');
                if(empty($UserID)){
                    $UserInfo = Db::name('user')->where(['wechat_openid'=>$user_openid])->find();
                    if(empty($UserInfo)){
                        $this->redirect('Open/Login',['wx'=>'wx']);
                    }else{
                        Session::set('user_id',$UserInfo['id']);
                        $this->redirect('Index/index');
                    }
                }else{
                    Db::name('user')->where(['id'=>Session::get('user_id')])->update(['wechat'=>$user_nickname,'wechat_openid'=>$user_openid]);
                    $this->redirect('Index/index');
                }




//                $cur_user = model('app\admin\model\User')
//                    ->where('open_id','=',$user_openid)
//                    ->find();
//
//                // 判断当前用户是否修改过信息,若有变动则更新
//                if(strcmp($cur_user->name,$user_nickname)!=0||strcmp($cur_user->headimgurl,$user_headimgurl)!=0){
//                    $cur_user->name = $user_nickname;
//                    $cur_user->headimgurl = $user_headimgurl;
//                    // 更新当前用户信息
//                    $cur_user->save();
//                }


            }

        }
    }
}